package com.imonsoft.sqm.service.basedata;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.dao.basedata.DictionaryDAO;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.DictionaryExample;


@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private DictionaryDAO dictionaryDAO;

    @Override
    public void deleteDictionary(List<Long> idList){
        if(null==idList || idList.isEmpty()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        for(int i=0;i<idList.size();i++){
            Dictionary dictionary=dictionaryDAO.selectDictionaryByPrimaryKey(idList.get(i));
            dictionary.setState(ConstantSqm.STATUS_DELETE);
            dictionaryDAO.updateDictionaryByPrimaryKey(dictionary);
        }
    }
    @Override
    public List<Dictionary> findDictionaryByType(String type) {
        if(StringUtils.isEmpty(type)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(!StringUtils.isEmpty(type)){
            criteria.andTypeEqualTo(type);
        }
        List<Dictionary> dictionaryList=dictionaryDAO.selectDictionaryByExample(example);
        return dictionaryList;
    }
    @Override
    public List<Dictionary> findDictionaryByCode(String code) {
        if(StringUtils.isEmpty(code)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(code)){
            criteria.andCodeEqualTo(code);
        }
        List<Dictionary> dictionaryList=dictionaryDAO.selectDictionaryByExample(example);
        return dictionaryList;
    }

    @Override
    public Long insert(Dictionary entity) {
        if(null==entity){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        entity.setState(ConstantSqm.STATUS_NORMAL);
        return dictionaryDAO.insertDictionary(entity);
    }

    @Override
    public int update(Dictionary entity) {
        if(null==entity || null==entity.getId()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Dictionary dictionary=dictionaryDAO.selectDictionaryByPrimaryKey(entity.getId());
        dictionary.setCode(entity.getCode());
        dictionary.setName(entity.getName());
        dictionary.setType(entity.getType());
        dictionary.setDetail(entity.getDetail());
        dictionary.setComment(entity.getComment());
        return dictionaryDAO.updateDictionaryByPrimaryKey(dictionary);
    }

    @Override
    public int delete(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        return dictionaryDAO.deleteDictionaryByPrimaryKey(id);
    }

    @Override
    public Dictionary get(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Dictionary dictionary=dictionaryDAO.selectDictionaryByPrimaryKey(id);
        return dictionary;
    }

    @Override
    public List<Dictionary> findList(Dictionary entity) {
        DictionaryExample example = new DictionaryExample();
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getType())){
                criteria.andTypeEqualTo(entity.getType());
            }
        }
        List<Dictionary> dictionaryList=dictionaryDAO.selectDictionaryByExample(example);
        return dictionaryList;
    }

    @Override
    public DataGrid<Dictionary> getDataGrid(Dictionary entity, Pager pager) {
        DictionaryExample example = new DictionaryExample();
        example.setOrderByClause("id desc");
        DictionaryExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if (null != entity && !StringUtils.isEmpty(entity.getType())) {
            criteria.andTypeLike(entity.getType()+"%");
        }
        return dictionaryDAO.getDataGrid(example, pager);
    }
    @Override
    public DataGrid<Dictionary> getDataGridDictionaryType(Pager pager) {
        return dictionaryDAO.getDictionaryType(pager);
    }
	@Override
	public Dictionary getDictionaryCodeType(String code, String type) {
		if(StringUtil.isBlank(code)||StringUtil.isBlank(type)){
			return null;
		}
		List<Dictionary> dictionaryList=queryAll();
		for(Dictionary d:dictionaryList){
			if(d.getCode().equals(code)&&d.getType().equals(type)&&d.getState().equals(ConstantSqm.STATUS_NORMAL)){
				return d;
			}
		}
		return null;
	}
    @Override
	public String getCodeByNameType(String name, String type) {
		if(StringUtil.isBlank(name)||StringUtil.isBlank(type)){
			return null;
		}
		List<Dictionary> dictionaryList=queryAll();
		for(Dictionary d:dictionaryList){
			if(d.getName().equals(name)&&d.getType().equals(type)&&d.getState().equals(ConstantSqm.STATUS_NORMAL)){
				return d.getCode();
			}
		}
		return null;
	}
    @Override
	public String getNameByCodeType(String code, String type) {
		if(StringUtil.isBlank(code)||StringUtil.isBlank(type)){
			return null;
		}
		List<Dictionary> dictionaryList=queryAll();
		for(Dictionary d:dictionaryList){
			if(d.getCode().equals(code)&&d.getType().equals(type)&&d.getState().equals(ConstantSqm.STATUS_NORMAL)){
				return d.getName();
			}
		}
		return null;
	}
	/**
	 * 查询所有
	 */
	@Override
	public List<Dictionary> queryAll() {
		DictionaryExample example = new DictionaryExample();
		List<Dictionary> dictionaryList = dictionaryDAO.selectDictionaryByExample(example);
		return dictionaryList;
	}
}
